<template>
  <el-dialog :visible.sync="addVisible" @close="$emit('update:addVisible',false)" width="55%" :show-close='false'   class="addQcPlanDialog"
	 :closeOnClickModal='false'>
		<span slot="title" class="dialog-title">
		  	{{newOrEdit=='new'?'新增':(newOrEdit=='edit'?'编辑':'查看')}}物资列表
			<el-button  size="mini" @click="close" class="popupClose">关闭</el-button>
		</span>
		<el-form label-width="110px" :inline="false" class="demo-form-inline" :model="materialForm" :rules="rules" ref="materialForm">
      <el-form-item label="物资类型" prop="materialTypeName" v-show="newOrEdit == 'edit'">
				<el-col :span="22" prop="materialTypeName">
					<el-input placeholder="物资类型" size="mini" v-model="materialForm.materialTypeName" disabled></el-input>
				</el-col>
			</el-form-item>
      <el-form-item label="物资类型"  v-show="newOrEdit == 'new'">
				<el-col :span="22">
					<el-input placeholder="物资类型" size="mini" v-model="materialTypeInfo.name" disabled></el-input>
				</el-col>
			</el-form-item>
    	<el-form-item label="名称" prop="name">
				<el-col :span="22">
					<el-input placeholder="物资名称" size="mini" v-model="materialForm.name"></el-input>
				</el-col>
			</el-form-item>
			<!--<el-form-item label="物资编号" prop="number">
				<el-col :span="22">
					<el-input placeholder="物资编号" size="mini" v-model="materialForm.number"></el-input>
				</el-col>
			</el-form-item>-->
	
			<el-form-item label="状态" v-show="newOrEdit != 'new'">
				<el-col :span="22">
					<el-select v-model="materialForm.status" placeholder="请选择" style="width: 100%;" size="mini" disabled>
						<el-option v-for="item in enableStatus" :key="item.value" :label="item.label" :value="item.value">
						</el-option>
					</el-select>
				</el-col>
			</el-form-item>

		</el-form>

		<span slot="footer" class="dialog-footer">
			<el-button type="primary" size="mini" @click="submitMaterType" :disabled="!isRepeat">提 交</el-button>
			<el-button  size="mini" @click="close">取 消</el-button>
		</span>
	</el-dialog>
		
</template>

<script>
	import { mapState } from 'vuex'


export default {
  data () {
    return {
      materialForm:{
        name:null,
        // number:null,

      },
      rules:{
        name: [
						{ type: 'string', required: true, message: '请填写名称', trigger: 'blur' }
					],
      },
			isRepeat:true,
    }
  },
  props:['addVisible','newOrEdit','marterialInfo','materialTypeInfo',],
  computed: {
			...mapState(['yesorno', 'enableStatus','submitType']),
	},

 
  mounted(){
    
  },
  watch:{
     addVisible(val, oldVal) {
				if (val) {
          
            if(this.newOrEdit == 'edit'){
              for (var i in this.marterialInfo){
                this.materialForm[i] = this.marterialInfo[i]
              }
            }
				}
			}
  },
  methods:{
    submitMaterType(){
        this.$refs.materialForm.validate((valid) => {  //表单验证
					if (valid) {
						var data = {
							name: this.materialForm.name,
              // number:this.materialForm.number,
              materialType:this.materialTypeInfo.code,
              submitType:this.submitType.submitOpen,
						}
						if (this.newOrEdit == 'new') {
							var url = "/technology/addMaterial.htm"

						} else {
							var url = '/technology/updateMaterial.htm';
							data.id = this.materialForm.id;
						}
						this.isRepeat = false;
						this.$http({
							method: 'post',
							apiUrl: url,
							params: data,
							isJson: true,
						}).then(res => {
							this.isRepeat = true;
							if (res.code == 0) {
								this.$message({
									type: 'success',
									message: '提交成功!'
								});
							this.$parent.readMaterialList(this.$parent.materQuery)
								this.close();
							}
						}).catch(() => {

						});

					} else {
						return false;
					}
				});
    },
     close(){
        this.$refs.materialForm.resetFields();//重置表单
				this.isRepeat = true;
				this.materialForm = {
					name: null,
          // number:null,
				};
				this.$emit('update:addVisible', false);
    },
  }
}
</script>

<style>
</style>